#import "UIScrollView+BCEmptyDataExt.h"
#import <BCComConfigKit/BCComConfig.h>
#import <objc/runtime.h>

@interface UIScrollView ()

@property (nonatomic, copy) EmptyBtnBlock bc_btnBlock;

@end

@implementation UIScrollView (BCEmptyDataExt)

///MARK: - Public
- (void)bc_emptyImage:(UIImage *)emptyImg title:(NSString *)emptyTitle {
    [self bc_emptyImage:emptyImg title:emptyTitle btnBlock:nil];
}

- (void)bc_emptyImage:(UIImage *)emptyImg title:(NSString *)emptyTitle btnBlock:(EmptyBtnBlock _Nullable)btnBlock {
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    if (btnBlock) {
        self.bc_btnBlock = btnBlock;
    }
    self.bc_emptyImage = emptyImg;
    self.bc_emptyTitle = emptyTitle;
    // 默认服务器正常
    self.bc_success = YES;
}

///MARK: - Private
- (BOOL)isNoNetwork {
    ///TODO: 添加网络状态判断
    return NO;
}
- (NSString *)prepareButtonTitle {
    if ([self isNoNetwork]) {
        return @"尝试重连";
    } else if (!self.bc_success) {
       return @"尝试刷新";
    } else {
       return self.bc_emptyBtnTitle;
    }
}

///MARK: - DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    // 优先判断无网络
    UIImage *emptyImg = nil;
    if ([self isNoNetwork]) {
        emptyImg = [UIImage imageNamed:@"bc_img_empty_network"];
    } else if (!self.bc_success) {
        // 判断是否w服务器异常
        emptyImg = [UIImage imageNamed:@"bc_img_empty_service"];
    } else {
        emptyImg = self.bc_emptyImage;
    }
    // 没有图片使用老的默认图
    if (!emptyImg) {
        emptyImg = [UIImage imageNamed:@"search_empty"];
    }
    return emptyImg;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"";
    if ([self isNoNetwork]) {
        title = @"网络连接失败";
    } else if (!self.bc_success) {
        title = @"网络连接失败";
    } else {
        title = self.bc_emptyTitle;
    }
    NSDictionary *attributes = @{ NSFontAttributeName: BCComConfig.config.bcFont(14),
                                  NSForegroundColorAttributeName: [UIColor colorWithRed:153 / 255. green:153 / 255. blue:153 / 255. alpha:1] };

    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *btnTitle = [self prepareButtonTitle];
    if (btnTitle.length < 1) {
        return nil;
    }
    NSDictionary *attributes = @{ NSFontAttributeName: BCComConfig.config.bcFont(15),
                                  NSForegroundColorAttributeName: BCComConfig.config.defaultColor };

    return [[NSAttributedString alloc] initWithString:btnTitle attributes:attributes];
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return 22;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return self.bc_offsetY;
}

///MARK: - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (self.bc_btnBlock) {
        self.bc_btnBlock();
    }
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.bc_showEmptyData;
}

- (void)emptyDataSetDidAppear:(UIScrollView *)scrollView {
    NSString *btnTitle = [self prepareButtonTitle];
    if (btnTitle.length < 1) {
        return ;
    }
    UIButton *button = [scrollView valueForKeyPath:@"bc_emptyDataView.button"];
    button.layer.borderWidth = 1.f;
    button.layer.borderColor = [BCComConfig.config.defaultColor CGColor];
    button.layer.cornerRadius = 20;
    if (button) {
        // Change button width
        for (NSLayoutConstraint *constraint in button.superview.constraints) {
            if (constraint.firstItem == button && constraint.firstAttribute == NSLayoutAttributeLeading) {
                constraint.constant = 140.0;
            } else if (constraint.secondItem == button && constraint.secondAttribute == NSLayoutAttributeTrailing) {
                constraint.constant = 140.0;
            }
        }
        // Change button height
        for (NSLayoutConstraint *constraint in button.constraints) {
            if (constraint.firstItem == button && constraint.firstAttribute == NSLayoutAttributeHeight) {
                constraint.constant = 40.0;
            }
        }
    }
}

///MARK: - get&set
- (EmptyBtnBlock)bc_btnBlock {
    return objc_getAssociatedObject(self, @selector(bc_btnBlock));
}

- (void)setBc_btnBlock:(EmptyBtnBlock)bc_btnBlock {
    objc_setAssociatedObject(self, @selector(bc_btnBlock), bc_btnBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGFloat)bc_offsetY {
    return [objc_getAssociatedObject(self, @selector(bc_offsetY)) floatValue];
}

- (void)setBc_offsetY:(CGFloat)bc_offsetY {
    objc_setAssociatedObject(self, @selector(bc_offsetY), @(bc_offsetY), OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)bc_emptyTitle {
    return objc_getAssociatedObject(self, @selector(bc_emptyTitle));
}

- (void)setBc_emptyTitle:(NSString *)bc_emptyTitle {
    objc_setAssociatedObject(self, @selector(bc_emptyTitle), bc_emptyTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)bc_emptyBtnTitle {
    return objc_getAssociatedObject(self, @selector(bc_emptyBtnTitle));
}

- (void)setBc_emptyBtnTitle:(NSString *)bc_emptyBtnTitle {
    objc_setAssociatedObject(self, @selector(bc_emptyBtnTitle), bc_emptyBtnTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIImage *)bc_emptyImage {
    return objc_getAssociatedObject(self, @selector(bc_emptyImage));
}

- (void)setBc_emptyImage:(UIImage *)bc_emptyImage {
    objc_setAssociatedObject(self, @selector(bc_emptyImage), bc_emptyImage, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)bc_success {
    return [objc_getAssociatedObject(self, @selector(bc_success)) boolValue];
}

- (void)setBc_success:(BOOL)bc_success {
    objc_setAssociatedObject(self, @selector(bc_success), @(bc_success), OBJC_ASSOCIATION_ASSIGN);
}
@end
